#include<iostream.h>
#include<fstream.h>

int main()
{	
	fstream file,file2,file3;
	char fname[50];
	int rows,cols;
	int i,j;
	float avg=0;
	int a,d,b,u;
	float h;
	int fact,cc;
	char fwrite[50];
	float **array;

	cout << "Type File name of List file: ";
	cin >> fname;

	cout << "Number of files in List file: ";
	cin >> cc;

	file.open(fname,ios::in);
	if(!file)
	{
		cout << "error reading file 1";
		return (1);
	}
	
	char **arrayf;
	arrayf = new char*[cc];

	for(i=0; i<cc; i++)
		arrayf[i] = new char[50];	

	for(u=0;u<cc;u++)
		file >> arrayf[u];
	file.close();

	cout << "How many rows : ";
	cin >> rows;
	cout<< "How many columns : ";
	cin >> cols;
	cout << "Enter the file name to be written : ";
	cin >> fwrite;
	file3.open(fwrite,ios::out);
/*	if(!file)
	{
		cout << "error writing file 2";
		return (1);
	}
	*/

	for(u=0;u<cc;u++)
	{
			
			cout << arrayf[u];
			file2.open(arrayf[u],ios::in);
			if(!file2)
			{
				cout << "error reading file 3";
				return (1);
			}
			

			

			array = new float*[rows];
			
			for(i=0; i<rows; i++)
				array[i] = new float [cols];


			for(i=0; i<rows; i++)
			{
				for(j=0; j<cols; j++)
					//file.read(reinterpret_cast<char*>(&array[i][j]),sizeof(float));
					file2 >> array[i][j];
			}

			do{
			cout << "Enter A (initial row number) ";

			cin >> a;
			}while(a<1);
			cout << "Enter D (common difference in rows) ";
			cin >> d;
			cout << "Enter First Col Value ";
			cin >> h;
			
			file3 << h << '\t';
			fact=int(((rows-a)/d)+1);
			for(b=3;b<cols;b++)
			{

				avg=0;
				for(j=(a-1);j<rows;j+=d)
				{
				avg+=array[j][b];
				}
				avg/=fact;
				file3 << avg << '\t';
			}
				file3 << '\n';
			// free the memory and exit
			for(i=0; i<rows; i++)
				delete(array[i]);
			delete(array);
			file2.close();
			
		}
	file3.close();
	return 0;

}